#include "idatabase.h"
#include "QMessageBox"
#include "QUuid"
#include "QSqlRecord"
void IDatabase::initDataBase()
{
    database=QSqlDatabase::addDatabase("QSQLITE"); //添加 SQLITE数据库驱动
    QString aFile ="../../../dbLab3.db";
    database.setDatabaseName(aFile);  //设置数据库文件
    qDebug() << QDir::currentPath();
    if (!database.open())
        qDebug()<<"faild to open database";
    else
        qDebug()<<"open database is ok";
}

bool IDatabase::initPatientModel()
{
    patientTabModel = new QSqlTableModel(this,database);
    patientTabModel->setTable("patient");
    patientTabModel->setEditStrategy(QSqlTableModel::OnManualSubmit);	//数据保存方式，OnManualSubmit , OnRowChange
    patientTabModel->setSort(patientTabModel->fieldIndex("name"),Qt::AscendingOrder); //排序
    if (!(patientTabModel->select()))	//查询数据失败
    {
        return false;
    }

    thePatientSelModel = new QItemSelectionModel(patientTabModel);
    return true;
}

int IDatabase::addNewPatient()
{
    // patientTabModel->insertRow(patientTabModel->rowCount(),
    //                            QModelIndex());//末尾添加记录
    // QModelIndex curIndex = patientTabModel->index(patientTabModel->rowCount()-1,1);

    // int curRecNo = curIndex.row();
    // QSqlRecord curRec = patientTabModel->record(curRecNo);//获取当前记录
    // curRec.setValue("createdTimeStamp",QDateTime::currentDateTime().toString("yyyy-MM-dd"));
    // curRec.setValue("id",QUuid::createUuid().toString(QUuid::WithoutBraces));

    // patientTabModel->setRecord(curRecNo,curRec);

    // return curIndex.row();

    patientTabModel->insertRow(patientTabModel->rowCount());
    int newRowIndex = patientTabModel->rowCount() - 1;

    // 直接通过模型设置字段值
    QModelIndex idIndex = patientTabModel->index(newRowIndex, patientTabModel->fieldIndex("id"));
    QModelIndex createdTimeStampIndex = patientTabModel->index(newRowIndex, patientTabModel->fieldIndex("createdTimeStamp"));

    patientTabModel->setData(idIndex, QUuid::createUuid().toString(QUuid::WithoutBraces));
    patientTabModel->setData(createdTimeStampIndex, QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"));

    // 其他字段可以根据需要设置默认值或保持为空，等待用户输入

    return newRowIndex;
}

bool IDatabase::searchPatient(QString filter)
{
    patientTabModel->setFilter(filter);
    return patientTabModel->select();
}

bool IDatabase::deletePatient()
{
    QModelIndex curIndex = thePatientSelModel->currentIndex();//获取当前选择单元格的模型索引
    patientTabModel->removeRow(curIndex.row());
    patientTabModel->submitAll();
    patientTabModel->select();
    return true;
}

bool IDatabase:: submitPatient()
{
    return patientTabModel->submitAll();
}

void IDatabase::revertPatient()
{
    patientTabModel->revertAll();
}


QString IDatabase::userLogin(QString userName, QString password)
{
    QSqlQuery query;
    query.prepare("select userName,password from user where userName = :USER");
    query.bindValue(":USER",userName);
    query.exec();

    if(query.first() && query.value("userName").isValid()){
        QString passwd = query.value("password").toString();
        if(passwd == password){
            qDebug()<<"login is Ok";
            return "login is Ok";
        }else{
            qDebug()<<"wrongPassword";
            return "wrongPassword";
        }
    }else{
        qDebug()<<"no such user";
        return "WrongUserName";
    }
}

IDatabase::IDatabase(QObject *parent)
    : QObject{parent}
{
    initDataBase();
}
